/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author stefan
 */
public class Util {

    public static final String SESSION_USER = "SessionUser";
    public static final String SESSION_USER_ID = "SessionUserID";
    private static Session session = null;

    public static Session getSesion() {
        if (session == null) {
            Configuration conf = new Configuration().configure("hibernate.cfg.xml");
            StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
            SessionFactory sessionFactory = conf.buildSessionFactory(ssr);
            session = sessionFactory.openSession();
        }        
        return session;
    }
    
    public static void invalidateSession(){
        if(session != null){
            session.close();
            session = null;
            Log.i("Hibernate sesija zatvorena ");
        }
    }

}
